package me.znext.learn.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

public class FingersActivity extends Activity {
  private DrawView drawView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fingers);
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.rootView);
    drawView = new DrawView(this);
    drawView.invalidate();
    layout.addView(drawView);
  }

  @Override
  public boolean onTouchEvent(MotionEvent motionEvent) {
    int pointerCount = motionEvent.getPointerCount();
    // 监听多点触控，不加上ActionMask 会导致一直都只有一个点
    int action = (motionEvent.getAction() & MotionEvent.ACTION_MASK) % 5;
    switch (action) {
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_MOVE:
        drawCircle(motionEvent, pointerCount);
        break;
    }
    return true;
  }

  private void drawCircle(MotionEvent event, int pointerCount) {
    float[][] circles = new float[pointerCount][2];
    for (int i = 0; i < pointerCount; ++i) {
      circles[i][0] = event.getX(i);
      circles[i][1] = event.getY(i);
    }
    drawView.updateCircles(circles);
  }
}
